package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //获取业务层对象
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private HttpSession session;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //获取会话id
        Long id = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);
        //调用业务方法
        setmealService.save(setmealDto);
        return R.success("添加成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> findSetmeal(Integer page, Integer pageSize, String name) {
        //调用分页查询方法
        Page<SetmealDto> setmealDtoPage = setmealService.findSetmeal(page, pageSize, name);
        //返回结果
        return R.success(setmealDtoPage);
    }

    //批量删除
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids) {
        Long id = (Long) session.getAttribute("employee");
        setmealService.deleteByIds(ids, id);
        return R.success("删除成功");
    }

    //根据id查询一个套餐和多个套餐下的菜品
    @GetMapping("/{id}")
    public R<SetmealDto> findById(Long id) {
        return R.success(setmealService.findById(id));
    }

    //获取列表数据
    @RequestMapping("/list")
    public R<List<Setmeal>> findByCategoryId(Long categoryId) {
        //调用用方法并返回结果
        return R.success(setmealService.findByCategoryId(categoryId));
    }
}
